package cn.angis.common.util;

import cn.hutool.core.date.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.PDFRenderer;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * 包名称：cn.angis.common.util
 * 类名称：PdfUtil
 * 类描述：
 * 创建人：@author angis.cn
 * 创建日期： 2023/11/30 16:28
 */
@Slf4j
public class PdfUtil {

    /**
     * dpi越大转换后越清晰，相对转换速度越慢
     */
    private static final Integer DPI = 100;

    /**
     * 转换后的图片类型
     */
    private static final String IMG_TYPE = "jpg";

    /**
     * PDF转图片
     *
     * @param fileContent PDF文件的二进制流
     * @return 图片文件的二进制流
     */
    public static List<byte[]> pdfToImage(byte[] fileContent) throws IOException {
        List<byte[]> result = new ArrayList<>();
        try (PDDocument document = PDDocument.load(fileContent)) {
            PDFRenderer renderer = new PDFRenderer(document);
            for (int i = 0; i < document.getNumberOfPages(); ++i) {
                BufferedImage bufferedImage = renderer.renderImageWithDPI(i, DPI);
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                ImageIO.write(bufferedImage, IMG_TYPE, out);
                result.add(out.toByteArray());
            }
        }
        return result;
    }

    /**
     * PDF转图片
     *
     * @param fileContent PDF文件的二进制流
     * @return 图片文件的二进制流
     */
    public static byte[] pdfToImage2(byte[] fileContent) throws IOException {
        byte[] result = null;
        try (PDDocument document = PDDocument.load(fileContent)) {
            PDFRenderer renderer = new PDFRenderer(document);
            BufferedImage bufferedImage = renderer.renderImageWithDPI(0, DPI);
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            ImageIO.write(bufferedImage, IMG_TYPE, out);
            result = out.toByteArray();
        }
        return result;
    }

    /**
     * @param imageBytes: 二进制数据
     * @param savePath:   保存的路径
     * @param imageName:  报告名称
     * @描述:
     **/
    public static Boolean saveImage(byte[] imageBytes, String savePath, String imageName) throws IOException {
        try {
            BufferedImage image = ImageIO.read(new ByteArrayInputStream(imageBytes));
            File directory = new File(savePath);
            if (!directory.exists()) {
                directory.mkdirs();
            }
            File outputFile = new File(directory, imageName);
            return ImageIO.write(image, "jpg", outputFile);
        } catch (Exception e) {
            log.error("保存图片报错", e);
            return false;
        }
    }

    public static String createDateDir(String basePath) throws Exception {

        String dayStr = DateUtil.format(DateUtil.date(Calendar.getInstance()), "yyyy-MM-dd");
        String[] dayArr = dayStr.split("-");

        String year = dayArr[0];
        String month = dayArr[1];
        String day = dayArr[2];

        String yearDir = basePath + File.separator + year;

        File yearFile = new File(yearDir);
        if (!yearFile.exists()) {
            yearFile.mkdirs();
        }

        String monthDir = yearDir + File.separator + month;
        File monthFile = new File(monthDir);
        if (!monthFile.exists()) {
            monthFile.mkdirs();
        }

        String dayDir = monthDir + File.separator + day;
        File dayFile = new File(dayDir);
        if (!dayFile.exists()) {
            dayFile.mkdirs();
        }
        //2020\10\15\
        return year + File.separator + month + File.separator + day + File.separator;
    }

}
